/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.zzz.view.managerControl;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import org.zzz.controller.*;
import org.zzz.entity.Teacher;
import org.zzz.entity.TeacherCourse;
import org.zzz.entity.TeacherVo;
import org.zzz.entity.vo.StudentShowVo;
import org.zzz.utils.Result;
import org.zzz.utils.ResultEnum;
import org.zzz.view.util.UiUtil;

import java.util.List;

/**
 *
 * @author AFun
 */
public class managerTeaSelect extends javax.swing.JFrame {

    /**
     * Creates new form managerTeaSelect
     */
    public managerTeaSelect() {
        initComponents();
        init();         //初始化
    }
    private void init(){
        this.setTitle("教师信息操作");
        UiUtil.setFrameCenter(this);
        this.setResizable(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox<>();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        chaxun = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("工号/姓名");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "工号", "姓名"}));

        jLabel2.setText("根据");

        jButton2.setText("取消");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("修改");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton3.setText("删除");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        chaxun.setText("查询");
        chaxun.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chaxunActionPerformed(evt);
            }
        });

        jTable1.setBackground(new java.awt.Color(214, 217, 223));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                        {null, null, null, null, null}
                },
                new String [] {
                        "工号", "姓名", "性别", "学历", "任课"
                }
        ) {
            boolean[] canEdit = new boolean [] {
                    false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(55, 55, 55)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jLabel2)
                                                        .addComponent(jLabel1))
                                                .addGap(48, 48, 48)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                .addComponent(chaxun))))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(68, 68, 68)
                                                .addComponent(jButton1)
                                                .addGap(58, 58, 58)
                                                .addComponent(jButton3)
                                                .addGap(70, 70, 70)
                                                .addComponent(jButton2)))
                                .addContainerGap(69, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(35, 35, 35))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(46, 46, 46)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(42, 42, 42)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel2)
                                        .addComponent(chaxun))
                                .addGap(35, 35, 35)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(29, 29, 29)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jButton1)
                                        .addComponent(jButton3)
                                        .addComponent(jButton2))
                                .addContainerGap(35, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        manager_teaMessage mtm=new manager_teaMessage();
        this.dispose();
        mtm.setVisible(true);
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        if(this.is_exist){
            managerTeaAlter mta=new managerTeaAlter(this.m_Nums,this.m_Name,
                    this.m_Sex,this.m_Degree,this.m_TeaCourse);
            this.dispose();
            mta.setVisible(true);
        }else{
            JOptionPane.showMessageDialog(this, "查无此人，让在下如何修改！");
        }

    }

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        if(this.is_exist){
            //询问是否删除？
            if (JOptionPane.showConfirmDialog(null, "是否删除？", "Message", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
//                //是：将文本框
//                System.out.println("删除");
                UserController userController = new UserController();
                TeacherCourseController teacherCourseController = new TeacherCourseController();
                //删除教师和用户
                userController.deleteUser(this.m_Nums,2);
                //删除教师课程关联
                Result<TeacherCourse> getScResult = teacherCourseController.getTCByTid(this.m_Nums);
                if(getScResult.getStatus()==ResultEnum.SUCCESS.getStatus()){
                    teacherCourseController.deleteTCById(getScResult.getData().getId());
                }

                JOptionPane.showMessageDialog(null, "删除成功！");
                DefaultTableModel model = (DefaultTableModel) this.jTable1.getModel();
                if(model.getColumnCount()!=0){
                    model.removeRow(0);
                }
                this.jTextField1.setText("");
            }
        }else{
            JOptionPane.showMessageDialog(this, "查无此人，让在下如何删除！");
        }

    }

    private void chaxunActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:

        //查找成功
        this.is_exist=true;

        //获取查找是否成功状态
        // TODO add your handling code here:
        // TODO add your handling code here:
        //获取表框内数据
        String messageXX=this.jTextField1.getText();
        String genju=this.jComboBox1.getSelectedItem().toString();

        //从数据库获取数据
        UserController userController = new UserController();
        TeacherController teacherController = new TeacherController();
        TeacherCourseController teacherCourseController = new TeacherCourseController();
        DefaultTableModel tableModel=(DefaultTableModel)this.jTable1.getModel();
        if(tableModel.getRowCount()!=0){
            tableModel.removeRow(0);
        }

        if("工号".equals(genju)){
            Result<TeacherVo> teacherById = userController.getTeacherById(messageXX);
            //存在该工号的信息
            if(teacherById.getStatus()==ResultEnum.SUCCESS.getStatus()){
                //查询成功

                TeacherVo data = teacherById.getData();
                this.m_Nums=messageXX;
                Result<String> courseNamesByTid = teacherCourseController.getCourseNamesByTid(this.m_Nums);
                this.m_Name=data.getName();
                this.m_Sex=data.getGender();
//                System.out.println(this.m_Sex);
                this.m_Degree=data.getDegree();
                this.m_TeaCourse=courseNamesByTid.getData();

                tableModel.addRow(new Object[]{this.m_Nums,this.m_Name,this.m_Sex,
                this.m_Degree,this.m_TeaCourse});
            }else{
                //查不到
                JOptionPane.showMessageDialog(this, "查询不到此人信息");
            }
        }else{
            //姓名
            Result<List<Teacher>> teacherByName = teacherController.getTeacherByName(messageXX);
            if(teacherByName.getStatus()==ResultEnum.SUCCESS.getStatus()){
                //查到
                List<Teacher> data = teacherByName.getData();
                for (Teacher datum : data) {
                    this.m_Nums=datum.getId();
                    Result<String> courseNamesByTid = teacherCourseController.getCourseNamesByTid(this.m_Nums);
                    this.m_Name=datum.getName();
                    this.m_Sex=datum.getGender();
                    this.m_Degree=datum.getDegree();
                    this.m_TeaCourse=courseNamesByTid.getData();
                    tableModel.addRow(new Object[]{this.m_Nums,this.m_Name,this.m_Sex,
                            this.m_Degree,this.m_TeaCourse});
                }
            }else{
                //查不到
                JOptionPane.showMessageDialog(this, "查询不到此人信息");
            }
        }


    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(managerTeaSelect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(managerTeaSelect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(managerTeaSelect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(managerTeaSelect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new managerTeaSelect().setVisible(true);
            }
        });
    }

    private String m_Nums;
    private String m_Name;
    private String m_Sex;
    private String m_Degree;
    private String m_TeaCourse;
    private boolean is_exist;


    // Variables declaration - do not modify
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton chaxun;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox<String> jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration
}
